﻿package 
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
		
	import cz.dadul.adventure.controller.*;
	import cz.dadul.adventure.view.*;
	import cz.dadul.adventure.asset.*;
	import cz.dadul.adventure.model.*;
	
	import gs.TweenMax;
	import gs.easing.*;
	/**
	 * ...
	 * @author Dadul
	 */
		 
	[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="30")]
	public class Main extends Sprite
	{
		// ----------- Imported Flash classes --------------------
		[Embed(source='../lib/resource.swf', symbol='animatedItem1')]
		private var itemImport1:Class;
		[Embed(source='../lib/resource.swf', symbol='animatedItem2')]
		private var itemImport2:Class;
		[Embed(source='../lib/resource.swf', symbol='animatedItem3')]
		private var itemImport3:Class;
		[Embed(source='../lib/resource.swf', symbol='animatedItem4')]
		private var itemImport4:Class;

		[Embed(source='../lib/resource.swf', symbol='player')]
		private var playerImport:Class;

		[Embed(source='../lib/resource.swf', symbol='frontLayer')]
		private var fl:Class;
		[Embed(source='../lib/resource.swf', symbol='mainLayer')]
		private var ml:Class;
		
		// -----------  Private vars --------------------	
		private var _mainController:MainController;
			
		
		public function Main():void 
		{
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			try{
				test();
			}
			catch (e:Error) {
				trace(e.message);				
				trace(e.getStackTrace());
			}
		}
		
		
		
		public function test():void {
			
			this._mainController = MainController.getInstance();
			
			var storageGraphics:MovieClip = new MovieClip();
			
			storageGraphics.graphics.beginFill(0xFF794B);
			storageGraphics.graphics.drawRect(0, 0, 800, 40);
			storageGraphics.graphics.endFill();
			storageGraphics.alpha = 0.2;
			
			var storage:Storage = new Storage(storageGraphics);		
			
			var player:Player = new Player(new playerImport());
				
			// Create layers			
			
			var mala:MainLayer = new MainLayer("mainLayer", new ml());
			var fala:Layer = new Layer("frontLayer", new fl()); 
			
			_mainController.addLayer(mala, true, 0, 100);
			_mainController.addLayer(fala, false, 0, 300);
		
		
			var item2:ClickableItem = new ClickableItem("item2", new itemImport2());
			var item3:PickableItem = new PickableItem("item3", new itemImport3());
			var item4:ClickableItem = new ClickableItem("item4", new itemImport4());

		
			_mainController.addMainItem(item2, 100, 50);
			_mainController.addMainItem(item3, 120, 100);
			_mainController.addMainItem(item4, 150, 150);
			
								
			// Assign logic and auto-dependencies
			item3.logic = ElementActionSet.addTimelineLogicSet(new Array(), new ElementActionSet().addAction("item3", "gotoAndStop", "testFrame"), "item2.lajna", "ohyb");
			
			item3.clickableLogic = ElementActionSet.addClickableLogicSet(new Array(), new ElementActionSet().addAction("item3", "play"));
										
			// Adds the main player
			_mainController.addPlayer(player, 300, 0);
			
			// Adds storage
			_mainController.addStorage(storage);
			
			_mainController.initStage(800, 600);
			// Adds only layer controller stage - VIEW
			addChild(_mainController.view);
			
			// Testing movement
			stage.addEventListener(KeyboardEvent.KEY_DOWN, player.moveByKey);
			
			// Start playing
			_mainController.start();			
		}
				
	
			
	}
}